﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pacocofose
{
    public class Instrucoes
    {
        #region Variaveis
        public static Song musicaInicioDoJogo;
        Texture2D Fundo_Instrucao, Creditos, Creditos_F, Instrucao, Instrucao_F, SinglePlayer, SinglePlayer_F, MultiPlayer, MultiPlayer_F, Quit, Quit_F, Tela_Creditos, Tela_Instrucao, SubMenu, Menu, Restart;
        public static bool Creditos2 = false;
        public static bool SinglePlayer2 = false;
        public static bool MultiPlayer2 = false;
        public static bool Quit2 = false;
        public static bool Instrucao2 = false;
        public static bool NaoAparecerContagem = false;
        bool SubMenu2 = false;
        bool AparecaContagem3 = false;
        bool AparecaContagem2 = false;
        bool AparecaContagem1 = false;
        public static bool Restart2 = false;
        bool Menu2 = false;
        bool JogoIniciado = false;
        bool DesaparecerMouse = false;
        bool Creditos21 = false;
        bool SinglePlayer21 = false;
        bool MultiPlayer21 = false;
        bool Quit21 = false;
        bool EmCimadoRestart = false;
        bool EmCimadoMenu = false;
        bool Instrucao21 = false;
        public static bool ResetarItens = false;
        public static bool ResetarPersonagens = false;
        public static bool ResetarTerreno = false;
        float timeRemaining = 0.0f;
        int timer = 2;
        public static float timeRemaining2 = 0.0f;
        public static int timer2 = 1;
        Rectangle SinglePlayerR, MultiPlayerR, InstrucaoR, CreditosR, QuitR, RestartR, MenuR;
        #endregion

        public Instrucoes(Game game)
        {
            #region Mouse / timer`s
            timeRemaining = timer;
            timeRemaining2 = timer2;
            game.IsMouseVisible = true;
            #endregion

            #region Texture2D
            Fundo_Instrucao = game.Content.Load<Texture2D>(@"Fundo");
            Creditos = game.Content.Load<Texture2D>(@"Creditos");
            Creditos_F = game.Content.Load<Texture2D>(@"Botao_Creditos_Seta_F");
            Instrucao = game.Content.Load<Texture2D>(@"Instrucao");
            Instrucao_F = game.Content.Load<Texture2D>(@"Botao_Instrução_Seta_F");
            SinglePlayer = game.Content.Load<Texture2D>(@"SinglePlayer");
            SinglePlayer_F = game.Content.Load<Texture2D>(@"Botao_SinglePlayer_Seta_F");
            MultiPlayer = game.Content.Load<Texture2D>(@"MultiPlayer");
            MultiPlayer_F = game.Content.Load<Texture2D>(@"Botao_MultiPlayer_Seta_F");
            Quit = game.Content.Load<Texture2D>(@"Quit");
            Quit_F = game.Content.Load<Texture2D>(@"Botao_Quit_Seta_F");
            Tela_Creditos = game.Content.Load<Texture2D>(@"Tela_Creditos");
            Tela_Instrucao = game.Content.Load<Texture2D>(@"Tela_Instrucao");
            SubMenu = game.Content.Load<Texture2D>(@"SubMenu");
            Menu = game.Content.Load<Texture2D>(@"Menu");
            Restart = game.Content.Load<Texture2D>(@"Restart");
            musicaInicioDoJogo = game.Content.Load<Song>(@"MusicaInicioDoJogo");
            #endregion
        }
        public void Update(Game game, GameTime gameTime)
        {
            #region Rectangle's
            SinglePlayerR = new Rectangle(245, 170, 190, 40);
            MultiPlayerR = new Rectangle(245, 240, 174, 40);
            CreditosR = new Rectangle(245, 314, 157, 37);
            InstrucaoR = new Rectangle(245, 380, 130, 36);
            QuitR = new Rectangle(245, 457, 70, 36);
            RestartR = new Rectangle(319, 250, 170, 40);
            MenuR = new Rectangle(350, 315, 100, 40);
            #endregion

            MouseState mouseState = Mouse.GetState();
            KeyboardState keyboardstate = Keyboard.GetState();

            #region Menus/Botões
            if (mouseState.X >= 245 && mouseState.X <= 435 && mouseState.Y >= 170 && mouseState.Y <= 210)
            {
                SinglePlayer21 = true;
                if (mouseState.LeftButton == ButtonState.Pressed && !Creditos2 && !Instrucao2 && !JogoIniciado)
                {
                    Itens.Item_supervelocidadeb = false;
                    Personagem.timeRemaining12 = Personagem.timer12;                    
                    MediaPlayer.Play(musicaInicioDoJogo);
                    Menu2 = false;
                    SubMenu2 = false;
                    Restart2 = false;
                    timeRemaining = timer;
                    timeRemaining2 = timer2;
                    JogoIniciado = true;
                    SinglePlayer2 = true;
                    DesaparecerMouse = true;
                }
            }
            else            
            {
                SinglePlayer21 = false;
            }

            if(JogoIniciado && Personagem.Contagem == false)
                Personagem.timeRemaining12 = MathHelper.Max(0, Personagem.timeRemaining12 - (float)gameTime.ElapsedGameTime.TotalSeconds);

            if (mouseState.X >= 245 && mouseState.X <= 419 && mouseState.Y >= 240 && mouseState.Y <= 280)
            {
                MultiPlayer21 = true;
                if (mouseState.LeftButton == ButtonState.Pressed && !Creditos2 && !Instrucao2 && !JogoIniciado)
                {
                    Itens.Item_supervelocidadeb = false;
                    Personagem.timeRemaining12 = Personagem.timer12;
                    MediaPlayer.Play(musicaInicioDoJogo);
                    Menu2 = false;
                    SubMenu2 = false;
                    Restart2 = false;
                    timeRemaining = timer;
                    timeRemaining2 = timer2;
                    JogoIniciado = true;
                    MultiPlayer2 = true;
                    DesaparecerMouse = true;
                }
            }
            else
            {
                MultiPlayer21 = false;
            }



            if (mouseState.X >= 245 && mouseState.X <= 402 && mouseState.Y >= 314 && mouseState.Y <= 351)
            {
                Instrucao21 = true;
                if (mouseState.LeftButton == ButtonState.Pressed && !Creditos2 && !JogoIniciado)
                {
                    Instrucao2 = true;
                }
            }
            else
            {
                Instrucao21 = false;
            }



            if (mouseState.X >= 245 && mouseState.X <= 375 && mouseState.Y >= 380 && mouseState.Y <= 416)
            {
                Creditos21 = true;
                if (mouseState.LeftButton == ButtonState.Pressed && !Instrucao2 && !JogoIniciado)
                {
                    Creditos2 = true;
                }
            }
            else
            {
                Creditos21 = false;
            }



            if (mouseState.X >= 245 && mouseState.X <= 315 && mouseState.Y >= 457 && mouseState.Y <= 493)
            {
                Quit21 = true;
                if (mouseState.LeftButton == ButtonState.Pressed && !Creditos2 && !Instrucao2 && !JogoIniciado)
                {
                    game.Exit();
                }
            }
            else
            {
                Quit21 = false;
            }

            if (DesaparecerMouse)
            {
                game.IsMouseVisible = false;
            }

            if (keyboardstate.IsKeyDown(Keys.Escape) && Instrucao2 || keyboardstate.IsKeyDown(Keys.Enter) && Instrucao2)
            {
                Instrucao2 = false;
            }

            if (keyboardstate.IsKeyDown(Keys.Escape) && Creditos2 || keyboardstate.IsKeyDown(Keys.Enter) && Creditos2)
            {
                Creditos2 = false;
            }
            #endregion

            #region SubMenu

            if (Personagem.VetorPersonagem.Y <= -69 && Personagem.VetorPersonagem2.Y <= -69 || Personagem.VetorPersonagem.Y <= -69 || Personagem.VetorPersonagem2.Y <= -69)
            {
                timeRemaining = MathHelper.Max(0, timeRemaining - (float)gameTime.ElapsedGameTime.TotalSeconds);
                if (timeRemaining == 0.0f && Restart2 == false && Menu2 == false)
                {
                    SubMenu2 = true;
                    Restart2 = false;
                    Menu2 = false;
                    timeRemaining = timer;
                }

                if (SubMenu2)
                {
                    game.IsMouseVisible = true;
                }

                if (mouseState.X >= 319 && mouseState.X <= 489 && mouseState.Y >= 250 && mouseState.Y <= 290)
                {
                    EmCimadoRestart = true;
                    if (mouseState.LeftButton == ButtonState.Pressed)
                    {
                        Itens.Item_supervelocidadeb = false;
                        Restart2 = true;
                        SubMenu2 = false;
                        EmCimadoRestart = false;
                        timeRemaining = timer;
                        MediaPlayer.Stop();
                        MediaPlayer.Play(musicaInicioDoJogo);
                    }
                }else
                    EmCimadoRestart = false;


                if (Restart2)
                {
                    timeRemaining2 = MathHelper.Max(0, timeRemaining2 - (float)gameTime.ElapsedGameTime.TotalSeconds);
                    if (timeRemaining2 == 0.0f)
                    {
                        Personagem.timeRemaining12 = Personagem.timer12;
                        NaoAparecerContagem = false;
                            ResetarItens = true;
                            ResetarPersonagens = true;
                            ResetarTerreno = true;
                            timeRemaining2 = timer2;
                            Restart2 = false;                        
                    }
                }

                if (mouseState.X >= 350 && mouseState.X <= 450 && mouseState.Y >= 315 && mouseState.Y <= 355)
                {
                    EmCimadoMenu = true;
                    if (mouseState.LeftButton == ButtonState.Pressed)
                    {
                        Menu2 = true;
                        SubMenu2 = false;
                        Restart2 = false;
                        EmCimadoMenu = false;
                        timeRemaining = timer;
                        MediaPlayer.Stop();
                    }
                }else
                    EmCimadoMenu = false;


                if (Menu2)
                {
                    timeRemaining2 = MathHelper.Max(0, timeRemaining2 - (float)gameTime.ElapsedGameTime.TotalSeconds);
                    if (timeRemaining2 == 0.0f)
                    {
                        NaoAparecerContagem = true;
                        Creditos2 = false;
                        MultiPlayer2 = false;
                        SinglePlayer2 = false;
                        Instrucao2 = false;
                        Restart2 = true;
                        ResetarItens = true;
                        ResetarPersonagens = true;
                        ResetarTerreno = true;
                        JogoIniciado = false;
                        Menu2 = false;
                    }
                }                
            }

            if (!MultiPlayer2 && !SinglePlayer2)
            {
                game.IsMouseVisible = true;
            }
            #endregion

            #region Contagem Inicial
            if (Personagem.timeRemaining12 > 2 && Personagem.timeRemaining12 < 2.9f)
            {
                AparecaContagem3 = true;
            }
            else
                AparecaContagem3 = false;


            if (Personagem.timeRemaining12 > 1f && Personagem.timeRemaining12 <= 2)
            {
                AparecaContagem2 = true;
            }
            else
                AparecaContagem2 = false;


            if (Personagem.timeRemaining12 <= 1f && Personagem.timeRemaining12 > 0f)
            {
                AparecaContagem1 = true;
            }
            else
                AparecaContagem1 = false;
#endregion
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            #region Renderizando imagens

            if (AparecaContagem3)
                spriteBatch.Draw(Personagem.Contagem3, new Vector2(0, 0), Color.White);

            if (AparecaContagem2)
                spriteBatch.Draw(Personagem.Contagem2, new Vector2(0, 0), Color.White);

            if (AparecaContagem1)
                spriteBatch.Draw(Personagem.Contagem1, new Vector2(0, 0), Color.White);

               
            if (!Creditos2 && !MultiPlayer2 && !SinglePlayer2 && !Instrucao2)
            {
                spriteBatch.Draw(Fundo_Instrucao, new Vector2(0, 0), Color.White);

                spriteBatch.Draw(Creditos_F, new Vector2(0, 0), Color.White);
                spriteBatch.Draw(Instrucao_F, new Vector2(0, 0), Color.White);
                spriteBatch.Draw(SinglePlayer_F, new Vector2(0, 0), Color.White);
                spriteBatch.Draw(MultiPlayer_F, new Vector2(0, 0), Color.White);
                spriteBatch.Draw(Quit_F, new Vector2(0, 0), Color.White);
                

                if (Creditos21)
                    spriteBatch.Draw(Creditos, new Vector2(0, -2), Color.White);
                if (Instrucao21)
                    spriteBatch.Draw(Instrucao, new Vector2(-2, 3), Color.White);
                if (SinglePlayer21)
                    spriteBatch.Draw(SinglePlayer, new Vector2(0, 0), Color.White);
                if (MultiPlayer21)
                    spriteBatch.Draw(MultiPlayer, new Vector2(0, 0), Color.White);
                if (Quit21)
                    spriteBatch.Draw(Quit, new Vector2(0, 0), Color.White);
            }
            if (Instrucao2)
            {
                spriteBatch.Draw(Tela_Instrucao, new Vector2(0, 0), Color.White);
            }
            if (Creditos2)
            {
                spriteBatch.Draw(Tela_Creditos, new Vector2(0, 0), Color.White);
            }

            if(SubMenu2 && !Restart2 && !Menu2)
            {
                spriteBatch.Draw(SubMenu, new Rectangle(280, 230, 250, 150), Color.White);
                spriteBatch.Draw(Menu, MenuR, Color.White);
                spriteBatch.Draw(Restart, RestartR, Color.White);

                if (EmCimadoMenu)
                {
                    spriteBatch.Draw(Menu, MenuR, Color.White);
                    spriteBatch.Draw(Menu, MenuR, Color.White);
                }
                if (EmCimadoRestart)
                {
                    spriteBatch.Draw(Restart, RestartR, Color.White);
                    spriteBatch.Draw(Restart, RestartR, Color.White);
                }
            }
            #endregion
        }
    }
}
